home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / windowsformsdemo / callerform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  6.9 KB  |  180 lines

  1. Public Class CallerForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents lblMessage As System.Windows.Forms.Label
  26.     Friend WithEvents btnShowModeless As System.Windows.Forms.Button
  27.     Friend WithEvents btnShowModal As System.Windows.Forms.Button
  28.     Friend WithEvents btnMessageBox As System.Windows.Forms.Button
  29.     Friend WithEvents Button1btnShowOwned As System.Windows.Forms.Button
  30.     
  31.     'Required by the Windows Form Designer
  32.     Private components As System.ComponentModel.Container
  33.  
  34.     'NOTE: The following procedure is required by the Windows Form Designer
  35.     'It can be modified using the Windows Form Designer.  
  36.     'Do not modify it using the code editor.
  37.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  38.         Me.btnMessageBox = New System.Windows.Forms.Button()
  39.         Me.btnShowModal = New System.Windows.Forms.Button()
  40.         Me.lblMessage = New System.Windows.Forms.Label()
  41.         Me.Button1btnShowOwned = New System.Windows.Forms.Button()
  42.         Me.btnShowModeless = New System.Windows.Forms.Button()
  43.         Me.SuspendLayout()
  44.         '
  45.         'btnMessageBox
  46.         '
  47.         Me.btnMessageBox.Location = New System.Drawing.Point(32, 120)
  48.         Me.btnMessageBox.Name = "btnMessageBox"
  49.         Me.btnMessageBox.Size = New System.Drawing.Size(280, 40)
  50.         Me.btnMessageBox.TabIndex = 0
  51.         Me.btnMessageBox.Text = "Show Message Box"
  52.         '
  53.         'btnShowModal
  54.         '
  55.         Me.btnShowModal.Location = New System.Drawing.Point(32, 168)
  56.         Me.btnShowModal.Name = "btnShowModal"
  57.         Me.btnShowModal.Size = New System.Drawing.Size(280, 40)
  58.         Me.btnShowModal.TabIndex = 0
  59.         Me.btnShowModal.Text = "Show Modal Form"
  60.         '
  61.         'lblMessage
  62.         '
  63.         Me.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  64.         Me.lblMessage.Location = New System.Drawing.Point(32, 232)
  65.         Me.lblMessage.Name = "lblMessage"
  66.         Me.lblMessage.Size = New System.Drawing.Size(280, 56)
  67.         Me.lblMessage.TabIndex = 1
  68.         '
  69.         'Button1btnShowOwned
  70.         '
  71.         Me.Button1btnShowOwned.Location = New System.Drawing.Point(32, 72)
  72.         Me.Button1btnShowOwned.Name = "Button1btnShowOwned"
  73.         Me.Button1btnShowOwned.Size = New System.Drawing.Size(280, 40)
  74.         Me.Button1btnShowOwned.TabIndex = 0
  75.         Me.Button1btnShowOwned.Text = "Show Modeless Owned Form"
  76.         '
  77.         'btnShowModeless
  78.         '
  79.         Me.btnShowModeless.Location = New System.Drawing.Point(32, 24)
  80.         Me.btnShowModeless.Name = "btnShowModeless"
  81.         Me.btnShowModeless.Size = New System.Drawing.Size(280, 40)
  82.         Me.btnShowModeless.TabIndex = 0
  83.         Me.btnShowModeless.Text = "Show Modeless Form"
  84.         '
  85.         'CallerForm
  86.         '
  87.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  88.         Me.ClientSize = New System.Drawing.Size(344, 309)
  89.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1btnShowOwned, Me.btnMessageBox, Me.btnShowModal, Me.lblMessage, Me.btnShowModeless})
  90.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  91.         Me.Name = "CallerForm"
  92.         Me.Text = "CallerForm"
  93.         Me.ResumeLayout(False)
  94.  
  95.     End Sub
  96.  
  97. #End Region
  98.  
  99.     Private Sub btnShowModeless_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowModeless.Click
  100.         ShowTheModelessForm()
  101.     End Sub
  102.  
  103.     Private Sub Button1btnShowOwned_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1btnShowOwned.Click
  104.         ShowTheOwnedForm()
  105.     End Sub
  106.  
  107.     Private Sub btnMessageBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessageBox.Click
  108.         ShowTheMessageBox()
  109.     End Sub
  110.  
  111.     Private Sub btnShowModal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowModal.Click
  112.         ShowTheModalForm()
  113.     End Sub
  114.  
  115.     ' show a non-modal form and get an event when it closes
  116.  
  117.     Sub ShowTheModelessForm()
  118.         ' Declare the Form object.
  119.         Dim frm As New CalleeForm()
  120.         ' Create a handler for the Closing event.
  121.         AddHandler frm.Closing, AddressOf CalleeForm_Closing
  122.         ' Show the form.
  123.         frm.Show()
  124.     End Sub
  125.  
  126.     ' this event fires when the nonmodal form closes 
  127.  
  128.     Private Sub CalleeForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
  129.         ' cast the sender argument to a specific object
  130.         Dim frm As CalleeForm = DirectCast(sender, CalleeForm)
  131.         ' display the value of two fields in CalleeForm
  132.         lblMessage.Text = frm.Text & " is closing" & ControlChars.CrLf
  133.         lblMessage.Text &= "  Name = " & frm.UserName & ControlChars.CrLf & "  Alias = " & frm.AliasName
  134.     End Sub
  135.  
  136.     ' show an owned form
  137.  
  138.     Sub ShowTheOwnedForm()
  139.         ' we use this variable to create unique captions for each form.
  140.         Static counter As Integer
  141.  
  142.         ' Declare the Form object.
  143.         Dim frm As New CalleeForm()
  144.         ' create a unique caption
  145.         counter += 1
  146.         frm.Text = "Callee Form #" & CStr(counter)
  147.  
  148.         ' Add this form to the list of owned form.
  149.         Me.AddOwnedForm(frm)
  150.         ' Create a handler for the Closing event.
  151.         AddHandler frm.Closing, AddressOf CalleeForm_Closing
  152.         ' Show the form.
  153.         frm.Show()
  154.     End Sub
  155.  
  156.     ' show a modal form
  157.  
  158.     Sub ShowTheModalForm()
  159.         ' Declare the Form object.
  160.         Dim frm As New CalleeForm()
  161.         ' Show the form modally
  162.         If frm.ShowDialog() = DialogResult.OK Then
  163.             ' display the value of two fields in CalleeForm
  164.             lblMessage.Text = "Name = " & frm.UserName & ControlChars.CrLf & "Alias = " & frm.AliasName
  165.         Else
  166.             ' the user canceled the dialog
  167.             lblMessage.Text = "The end user canceled the action"
  168.         End If
  169.     End Sub
  170.  
  171.     ' show a MessageBox 
  172.  
  173.     Sub ShowTheMessageBox()
  174.         Dim res As DialogResult
  175.         res = MessageBox.Show("File not found", "A MessageBox example", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
  176.         lblMessage.Text = "The user pressed " & res.ToString
  177.     End Sub
  178.  
  179. End Class
  180.